﻿//
//
//
function getShapeData(element) {
	
	var shapedata = "";
	element.beginEdit();
	
	var shapeArray = new Array();
	
	for (var i=0; i<element.contours.length; i++) {
		
		if (element.contours[i].interior) {
			
			var halfedge = element.contours[i].getHalfEdge();
			var backward = getDirection(halfedge);
			var startid = halfedge.id;
			var id = 0;
			
			while (startid != id) {
				
				var edge = halfedge.getEdge();
				
				var shapeObj = new Object();
				
				if (edge.isLine) {
					
					var point2 = edge.getControl(2);
					
					var x2 = Math.round(point2.x * 20) / 20;
					var y2 = Math.round(point2.y * 20) / 20;
					
					var point2b;
					
					if (backward) {
						point2b = halfedge.getPrev().getEdge().getControl(2);
					} else {
						point2b = halfedge.getNext().getEdge().getControl(2);
					}
					if (x2 == point2b.x && y2 == point2b.y) {
						
						fl.trace("WARNING: node x:" + x2 + ", y:" + y2 + " is repeated, attempting to correct.");

						if (!backward) {
							point2b = halfedge.getPrev().getEdge().getControl(0);
						} else {
							point2b = halfedge.getNext().getEdge().getControl(0);
						}
					
						var x2b = Math.round(point2b.x * 20) / 20;
						var y2b = Math.round(point2b.y * 20) / 20;						
					
					}
					
					shapeObj.x = x2;
					shapeObj.y = y2;
					
					if (id == 0) {
						var startx = x2;
						var starty = y2;
					}
					
				} else {
					
					var point1 = edge.getControl(1);
					var point2 = edge.getControl(2);
					
					var x1 = Math.round(point1.x * 20) / 20;
					var y1 = Math.round(point1.y * 20) / 20;
					var x2 = Math.round(point2.x * 20) / 20;
					var y2 = Math.round(point2.y * 20) / 20;
					
					shapeObj.x = x1;
					shapeObj.y = y1;
					
					if (id == 0) {
						var startx = x2;
						var starty = y2;
					}
					
				}
				
				if (backward) {
					halfedge = halfedge.getPrev();
				} else {
					halfedge = halfedge.getNext();
				}
				
				id = halfedge.id;
				
				shapeArray.push(shapeObj);
				
			}
			
			shapeObj = new Object();
			shapeObj.x = startx;
			shapeObj.y = starty;
			shapeArray.push(shapeObj);
			
		}
		
	}
	
	element.endEdit();
	
	shapeArray = fitShape(shapeArray);
	
	var shapeDataArray = [];

	for (var i = 0; i < shapeArray.length; i++) {
		shapeDataArray.push(shapeArray[i].x, shapeArray[i].y);
	}
		
	var shapedata = "[" + shapeDataArray.join(", ") + "]";
	
	return shapedata;
	
}

//
//
//
function getDirection(halfedge) {
	
	var edge = halfedge.getEdge();
	var nexthalfedge = halfedge.getNext();
	var nextedge = nexthalfedge.getEdge();
	var control = edge.getControl(2);
	var nextcontrol = nextedge.getControl(0);
	
	return (control.x == nextcontrol.x && control.y == nextcontrol.y);
	
}

//
//
function fitShape (p) {
	
	var xmax = -10000;
	var xmin = 10000;
	var ymax = -10000;
	var ymin = 10000;
	var cx = 0;
	var cy = 0;
	var scale = 1;

	for (var i = 0; i < p.length; i++) {
		
		xmax = Math.max(xmax, p[i].x);
		xmin = Math.min(xmin, p[i].x);
		ymax = Math.max(ymax, p[i].y);
		ymin = Math.min(ymin, p[i].y);
		
	}
	
	cx = xmin + (xmax - xmin) * 0.5;
	cy = ymin + (ymax - ymin) * 0.5;
		
	scale = 1 / Math.max(xmax - xmin, ymax - ymin);
	
	for (var i = 0; i < p.length; i++) {
		
		p[i].x -= cx;
		p[i].y -= cy;
		p[i].x *= scale;
		p[i].y *= scale;
		p[i].x = Math.floor(p[i].x * 100) / 100;
		p[i].y = Math.floor(p[i].y * 100) / 100;
		
	}	
	
	return p
	
}

fl.outputPanel.clear();	

var elementID = 0;
var code = "// SHAPE DATA \n\n";

var selArray = fl.getDocumentDOM().selection;
var elt = selArray[0];

if (elt != undefined) {
	
	var contourArray = elt.contours;
	var nodedata = getShapeData(elt);

	code += 'public static const ICON_' + elementID + ':Array = ' + nodedata + ';';
	code += '\n';

	fl.trace(code);
	fl.trace("// END SHAPE DATA");

} else {
	
	fl.trace("Nothing selected!");
	
}

